import Vue from 'vue'
import VueRouter from 'vue-router'
//导入视图
// import Login from '@/views/login'
// import Layout from '@/views/layout'
// import Search from '@/views/search'
// import SearchList from '@/views/search/list'
// import ProDetail from '@/views/prodetail'
// import Pay from '@/views/pay'
// import MyOrder from '@/views/myorder'



import Home from '@/views/layout/home'
import Category from '@/views/layout/category'
import Cart from '@/views/layout/cart'
import User from '@/views/layout/user'
import store from '@/store'
// import { getProDetail } from '@/api/product'
const Login=()=>import('@/views/login')
const Layout=()=>import('@/views/layout')
const Search=()=>import('@/views/search') 
const SearchList=()=>import('@/views/search/list')
const ProDetail=()=>import('@/views/prodetail')
const Pay=()=>import('@/views/pay')
const MyOrder=()=>import('@/views/myorder')


Vue.use(VueRouter)

// router中配置路由
const router = new VueRouter({
	routes: [
		{ path: '/login', component: Login },
		{
			path: '/',
			component: Layout,
			//访问/时重定向到home
			redirect: '/home',
			children: [
				{ path: '/home', component: Home },
				{ path: '/category', component: Category },
				{ path: '/cart', component: Cart },
				{ path: '/user', component: User },
			]
		},
		{ path: '/search', component: Search },
		{ path: '/searchlist', component: SearchList },
		//动态路由传参，路由参数中携带id
		{ path: '/prodetail/:id', component: ProDetail },
		{ path: '/pay', component: Pay },
		{ path: '/myorder', componen: MyOrder }
	],


})

//全局前置导航守卫（购物车必须登录）
//to: 到哪里去
//from :从哪里来
//next()表示直接放行到to坐标
//next(路径)表示进行拦截

//定义一个数组，专门存放所有需要权限访问的页面
const authUrls=['/pay','/myorder']
router.beforeEach((to, from, next) =>{
	// console.log('前置守卫');
	// console.log(to, from, next)
	// next()
	if(!authUrls.includes(to.path)){  //只考虑path不考虑fullpath
		next()
		return 
	}
	//权限页面需要判断token
	const token=store.getters.token
	
	if(token){
		next()
	}else{
		next('/login')
	}
	
})

export default router